﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace NCKH.Personal
{
    public partial class Hoithao_NNC : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                hientenhoithao();
                int taikhoan = laytaikhoan(Name);
                {
                    btndangki.Visible = true;
                }
            }
        }
        protected int laytaikhoan(string Name)
        {
            string connectionString = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            string selectTK = "SELECT MaTK FROM TAIKHOAN where Tendangnhap='" + Name + "'";
            using (SqlConnection Cnn = new SqlConnection(connectionString))
            {
                using (SqlCommand Cmd = new SqlCommand(selectTK, Cnn))
                {
                    using (SqlDataAdapter da = new SqlDataAdapter(Cmd))
                    {
                        DataTable tbl = new DataTable();
                        da.Fill(tbl);
                        DataRow dr = tbl.Rows[0];
                        object matk = dr["MaTK"];
                        int taikhoan = (int)matk;
                        return taikhoan;
                    }
                }
            }
        }
        private void hientenhoithao()
        {
            string sqlSelect = "SELECT MaHT,TenHT FROM HOITHAO ";
            string connectionString = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            using (SqlConnection Cnn = new SqlConnection(connectionString))
            {
                using (SqlCommand Cmd = new SqlCommand(sqlSelect, Cnn))
                {
                    Cnn.Open();
                    using (SqlDataReader Rd = Cmd.ExecuteReader())
                    {
                        ddrtenhoithao.Items.Insert(0, "-- Chọn tên hội thảo --");
                        while (Rd.Read())
                        {
                            ddrtenhoithao.Items.Add(
                                new ListItem(Rd["TenHT"].ToString(),
                                    Rd["MaHT"].ToString()));
                        }
                    }
                    Cnn.Close();
                }
            }
        }
        protected void NNC_HT_Insert()
        {
            string connection = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            using (SqlConnection cnn = new SqlConnection(connection))
            {
                using (SqlCommand cmd = new SqlCommand("NNC_HT", cnn))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@MaNNC", txtmannc.Text);
                    cmd.Parameters.AddWithValue("@TenNNC", txttennnc.Text);
                    cmd.Parameters.AddWithValue("@Diachi", txtdc.Text);
                    cmd.Parameters.AddWithValue("@ChucdanhKH", txtcdkh.Text);
                    cmd.Parameters.AddWithValue("@Chucvu", txtcv.Text);
                    cnn.Open();
                    cmd.ExecuteNonQuery();
                    cnn.Close();
                }
            }
        }
        protected void SV_HT_Insert()
        {
            string connection = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            using (SqlConnection cnn = new SqlConnection(connection))
            {
                using (SqlCommand cmd = new SqlCommand("Nguoithamgia_HT", cnn))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@Thoigiandangky", txttgdk.Text);
                    cmd.Parameters.AddWithValue("@Vaitro", txtvaitro.Text);
                    cnn.Open();
                    cmd.ExecuteNonQuery();
                    cnn.Close();
                }
            }
        }
        private void thongbao(string Noidung)
        {

            ScriptManager.RegisterStartupScript(this, this.GetType(), "UpdateCom", "alert('" + Noidung + "');return false;", true);
        }
        protected void btndangki_Click(object sender, EventArgs e)
        {
            NNC_HT_Insert();
            SV_HT_Insert();
            thongbao("Bạn đã đăng kí thành công");
        }

        protected void btnhuy_Click(object sender, EventArgs e)
        {
            txtcdkh.Text = txtcv.Text = txtdc.Text = txttennnc.Text = txttgdk.Text = txtvaitro.Text = string.Empty;
        }

        public string Name { get; set; }
    }
}